#include "VerifyGrpcClient.h"
#include <memory>
#include "ConfigMgr.h"


GetVerifyCodeResponse VerifyGrpcClient::GetVerifyCode(std::string email)
{
    ClientContext context;
    GetVerifyCodeRequest request;
    GetVerifyCodeResponse response;
    request.set_email(email);
    //从连接池重获取连接
    auto stub = pool_->GetConnection();
    Status status = stub->GetVerifyCode(&context,request,&response);
    
    //使用RAII机制，确保连接在函数退出时被返回
    Defer defer([&stub,this](){
        pool_->ReturConnection(std::move(stub));
    });

    if(!status.ok())
    {
        std::cout << "error: "<< status.error_message() << std::endl;
        response.set_error(ErrorCodes::RPCFailed);
        return response;
    }
    response.set_error(ErrorCodes::Success);
    return response;

}


VerifyGrpcClient::VerifyGrpcClient()
{
    auto & configMgr = ConfigMgr::GetInstance();
    std::string host = configMgr["VerifyServer"]["Host"];
    std::string port = configMgr["VerifyServer"]["Port"];
    pool_=std::make_unique<RPConPool>(5,host,port);
}
